<template>
  <div id="app">
    <van-nav-bar
      title="不凡"
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
    />
    <van-search v-model="ipt" placeholder="搜索" />
    <ul>
      <li v-for="item in list" :key="item.id">
        {{ item.title }}
      </li>
    </ul>
    <p>显示当前位置:</p>
    <div id="allmap"></div>
  </div>
</template>

<script type="text/javascript">
export default {
  data() {
    return {
      ipt: "",
      map: "",
      list: [],
    };
  },
  watch: {
    //监听输入框的改变
    ipt() {
      var that = this;
      var options = {
        onSearchComplete: function (results) {
          // 判断状态是否正确
          if (local.getStatus() == BMAP_STATUS_SUCCESS) {
            console.log(results.Hr);
            that.list = results.Hr;
            console.log(that.list, "1233");
          }
        },
      };
      var local = new BMap.LocalSearch(this.map, options);
      local.search(this.ipt);
    },
  },
  methods: {
    onClickLeft() {
      // this.$router.replace({  path: "/subpagest" });//不会向 history 添加新记录，而是跟它的方法名一样 —— 替换掉当前的 history 记录;
      // this.$router.push({  path: "/subpagest" });//会向 history 栈添加一个新的记录;
      // this.$router.go(-1);//这个方法的参数是一个整数，意思是在 history 记录中向前或者后退多少步。
      this.$router.replace("/");
    },
  },
  computed: {},
  created() {},
  mounted() {
    // 百度地图API功能
    var that = this;
    var map = new BMap.Map("allmap");
    this.map = map;
    var point = new BMap.Point(116.331398, 39.897445);
    map.centerAndZoom(point, 12);

    var geolocation = new BMap.Geolocation();
    geolocation.getCurrentPosition(
      function (r) {
        if (this.getStatus() == BMAP_STATUS_SUCCESS) {
          var mk = new BMap.Marker(r.point);
          map.addOverlay(mk);
          map.panTo(r.point);
          //搜索功能
          var options = {
            onSearchComplete: function (results) {
              // 判断状态是否正确
              if (local.getStatus() == BMAP_STATUS_SUCCESS) {
                // console.log(results);
              }
            },
          };
          var local = new BMap.LocalSearch(map, options);
          local.search(that.ipt);
        } else {
          alert("failed" + this.getStatus());
        }
      },
      { enableHighAccuracy: true }
    );
  },
};
</script>

<style scoped>
* {
  margin: 0;
  padding: 0;
}
#allmap {
  height: 230px;
}
.searchipt {
  width: 100%;
  height: auto;
  background-color: red;
}
p {
  height: 30px;
  font-size: 18px;
  font-weight: bold;
  color: #000;
  text-align: left;
  margin-left: 10px;
}
ul {
  height: 300px;
}
li {
  margin-left: 12px;
  text-align: left;
  height: 30px;
}
ul li:nth-child(1){
  margin-top:10px ;
}
.van-nav-bar{
  height: 50px;
  border-bottom: 3px solid #F4F4F4;
  margin-bottom: 5px;
}
.van-search{
  width: 350px;
  margin: 0 auto;
  border: 2px solid #F4F4F4;
  border-radius: 3px;
}
</style>
